package org.example.servicelisten.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;

import jakarta.websocket.server.ServerEndpointConfig;

@Configuration
public class WebSocketConfig implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    /**
     * 用于扫描和注册@ServerEndpoint注解的Bean
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    /**
     * 自定义Tomcat WebSocket配置
     */
    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers(connector -> {
            // 设置WebSocket连接超时时间为60分钟
            connector.setProperty("connectionTimeout", "3600000");
            // 设置会话超时时间为60分钟
            connector.setProperty("sessionTimeout", "3600000");
            // 设置保持连接超时
            connector.setProperty("keepAliveTimeout", "60000");
            // 禁用连接保持活跳检查
            connector.setProperty("maxKeepAliveRequests", "1000");
        });
    }
}